import request from "../util/request.ts";
import {Dictionary} from "./type/model.ts";

/**
 * 分页查询字典列表
 * @param currentPage 当前页码
 * @param pageSize 每页大小
 * @param dictionary 查询条件
 * @param during
 * @returns 字典列表及分页信息
 */
export const fetchDictionaryPage = async (currentPage: number, pageSize: number, dictionary: Dictionary, during: []): Promise<any> => {
    return await request.get(`/dictionary/page`, {
        params: {
            currentPage,
            pageSize,
            dictionary: JSON.stringify(dictionary),
            during: during?during.join(','):[]
        }
    });
}


/**
 * 添加字典
 * @param dictionary 字典对象
 * @returns 添加结果
 */
export const postDictionary = async (dictionary: Dictionary): Promise<any> => await request.post("/dictionary", dictionary);

/**
 * 更新字典
 * @param dictionary 字典对象
 * @returns 更新结果
 */
export const putDictionary = async (dictionary: Dictionary): Promise<any> => await request.put("/dictionary", dictionary);

/**
 * 删除字典
 * @param id 字典 ID
 * @returns 删除结果
 */
export const delDictionary = async (id: number): Promise<any> => await request.delete(`/dictionary/${id}`);

/**
 * 批量删除字典
 * @param ids 字典 ID 数组
 * @returns 删除结果
 */
export const batchDelDictionary = async (ids: number[]): Promise<any> => await request.delete(`/dictionary/batch/delete`, {
    params: {
        ids: ids.join(',')
    }
});

/**
 * 导出字典数据
 * @returns 导出的文件
 */
export const exportDictionaryData = async (): Promise<any> => await request.get("/dictionary/export", { responseType: "blob" });

/**
 * 获取字典详情
 * @param id 字典 ID
 * @returns 字典详情
 */
export const fetchDictionaryById = async (id: number): Promise<any> => await request.get(`/dictionary/${id}`);
